package com.example.joke.select

import android.app.Activity
import android.content.Intent
import androidx.fragment.app.Fragment

// 中间层
class ImageSelector private constructor(){

    // 原始的图片
    private var mOriginList: ArrayList<String>? = null
    private var mShowCamera: Boolean = true
    private var mMaxCount: Int = 9
    private var mMode: Int = MODE_MULTI

    companion object {
        fun create(): ImageSelector {
            return ImageSelector()
        }
    }

    public fun maxCount(maxCount: Int): ImageSelector {
        mMaxCount = maxCount
        return this
    }

    public fun showCamera(showCamera: Boolean): ImageSelector {
        mShowCamera = showCamera
        return this
    }

    public fun mode(mode: Int): ImageSelector {
        mMode = mode
        return this
    }

    public fun origin(list: ArrayList<String>): ImageSelector {
        mOriginList = list
        return this
    }

    fun start(activity: Activity, requestCode: Int) {
        val intent = Intent(activity, SelectImageActivity::class.java)
        addParams(intent)
        activity.startActivityForResult(intent, requestCode)
    }

    fun start(fragment: Fragment, requestCode: Int) {
        val intent = Intent(fragment.context, SelectImageActivity::class.java)
        addParams(intent)
        fragment.startActivityForResult(intent, requestCode)
    }

    private fun addParams(intent: Intent) {
        intent.putExtra(EXTRA_SELECT_COUNT, mMaxCount)
        intent.putExtra(EXTRA_SELECT_MODE, mMode)
        if (mOriginList != null && mMode == MODE_MULTI) {
            intent.putStringArrayListExtra(EXTRA_DEFAULT_SELECT_LIST, mOriginList)
        }
        intent.putExtra(EXTRA_SHOW_CAMERA, mShowCamera)
    }

}